package com.lanou.controller;

import com.github.pagehelper.PageInfo;
import com.lanou.entity.Course;
import com.lanou.entity.CourseCollect;
import com.lanou.service.CourseCollectService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CourseCollectController
 * @Description TODO
 * @Author cuiruitong
 * @Date 2025年08月13日 11:46
 * @Version 1.0
 */
@RestController
@RequestMapping("courseCollect")
public class CourseCollectController {
    @Autowired
    private CourseCollectService courseCollectService;
    //增加收藏课程
    @PostMapping
    public R addCourseCollect(@RequestBody CourseCollect courseCollect) {
        courseCollect.setStatus("未支付");
        int i = courseCollectService.addCourseCollect(courseCollect);
        if(i>0){
            return R.ok("收藏成功");
        }else {
            return R.fail("添加失败");
        }
    }
    //根据用户id和课程id删除收藏的课程
    @DeleteMapping
    public R deleteCourseCollect(@RequestBody CourseCollect courseCollect) {
        int i = courseCollectService.deleteCourseCollect(courseCollect);
        if(i>0){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }
    //根据用户id和课程查询单个收藏
    @GetMapping
    public R getCourseCollectByUserIdAndCourseId(CourseCollect courseCollect) {
        CourseCollect courseCollectByCidAndUId= courseCollectService.getCourseCollectByCidAndUId(courseCollect);
        return R.ok(courseCollectByCidAndUId);
    }
    //根据用户id查询所有收藏并分页
    @GetMapping("getPageById")
    public R getCourseCollectPageByUserId(int id,@RequestParam(required = false,defaultValue = "1") int pageNum,@RequestParam(required = false,defaultValue = "10") int pageSize) {
        PageInfo<CourseCollect> courseCollectByUserIdPage = courseCollectService.getCourseCollectByUserIdPage(id,pageNum,pageSize);
        return R.ok(courseCollectByUserIdPage);
    }

    //修改支付状态根据用户id和课程修改支付状态
    @PutMapping
    public R updateCourseCollect(@RequestBody CourseCollect courseCollect) {
        int i = courseCollectService.updateCourseCollect(courseCollect);
        if(i>0){
            return R.ok();
        }else {
            return R.fail("支付失败");
        }
    }

    @GetMapping("getCourseCollectByUserId")
    public R getCourseCollectByUserId(Integer userId,String status,@RequestParam(required = false,defaultValue = "1") int pageNum,@RequestParam(required = false,defaultValue = "10") int pageSize) {

        PageInfo<Course> collectCoursePage = courseCollectService.getCollectCourseByUserId(userId,status,pageNum, pageSize);

        return R.ok(collectCoursePage);
    }


}
